package com.topica;

import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Window;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.Toast;

import com.topica.R;
import com.topica.view.TopicaVideoView;

public class VideoActivity extends Activity {

	public static final String PATH = "com.topica.VideoActivity.PATH";
	private TopicaVideoView videoView;
	private int displayWidth;
	private int displayHeight;
	private int smallHeight;
	private int initialOrientataion;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_video);
		String videoPath = getIntent().getStringExtra(PATH);
		System.out.println("video path=" + videoPath);
		if(null == videoPath || videoPath.equals(null) || videoPath.equals("")){
			Toast.makeText(this, R.string.no_video, Toast.LENGTH_SHORT).show();
			finish();
			return;
		}
		DisplayMetrics metrics = getResources().getDisplayMetrics();
		displayHeight = metrics.heightPixels;
		displayWidth = metrics.widthPixels;
		smallHeight = metrics.heightPixels / 2;
		videoView = (TopicaVideoView) findViewById(R.id.video);
		initialOrientataion = getResources().getConfiguration().orientation;
		if (initialOrientataion == Configuration.ORIENTATION_PORTRAIT) {
			videoView.setDimensions(displayWidth, smallHeight);
		} else {
			videoView.setDimensions(displayWidth, displayHeight);
		}
		playVideo(videoPath);
	}

	private void playVideo(String path) {
		getWindow().setFormat(PixelFormat.TRANSLUCENT);
		videoView.setMediaController(new MediaController(this));
		videoView.setVideoURI(Uri.parse(path));
		videoView.requestFocus();
		videoView.start();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		if (initialOrientataion == Configuration.ORIENTATION_PORTRAIT) {
			if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
				getWindow().clearFlags(
						WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
				getWindow().setFlags(
						WindowManager.LayoutParams.FLAG_FULLSCREEN,
						WindowManager.LayoutParams.FLAG_FULLSCREEN);
				videoView.setDimensions(displayHeight, displayWidth);
				videoView.getHolder().setFixedSize(displayHeight, displayWidth);
			} else {
				getWindow().setFlags(
						WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
						WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
				videoView.setDimensions(displayWidth, smallHeight);
				videoView.getHolder().setFixedSize(displayWidth, smallHeight);
			}
		} else {
			if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
				getWindow().clearFlags(
						WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
				getWindow().setFlags(
						WindowManager.LayoutParams.FLAG_FULLSCREEN,
						WindowManager.LayoutParams.FLAG_FULLSCREEN);
				videoView.setDimensions(displayWidth, displayHeight);
				videoView.getHolder().setFixedSize(displayWidth, displayHeight);
			} else {
				getWindow().setFlags(
						WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
						WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
				videoView.setDimensions(displayHeight, displayWidth / 2);
				videoView.getHolder().setFixedSize(displayHeight,
						displayWidth / 2);
			}
		}
	}
}
